<template>
  <div>
    <Category :key_attr="currentPage"></Category>
    <el-card style="margin: 10px 0px">
      <div v-show="currentPage === 0">
        <el-button type="primary" icon="Plus" :disabled="isButtonDisabled" @click="saveOrUpdate">添加属性</el-button>
        <el-table border style="margin: 10px 0px" :data="attrArr">
          <el-table-column prop="id" label="序号" width="80" />
          <el-table-column prop="attrName" label="属性名称" width="120px" />
          <el-table-column prop="address" label="属性值名称">
            <template #default="scope">
              <el-tag v-for="item in scope.row.attrValueList" :key="item.id" style="margin: 5px">
                {{ item.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" width="240px">
            <template #default="scope">
              <el-button type="primary" icon="Edit" size="small" @click="saveOrUpdate">编辑</el-button>
              <el-button type="danger" icon="Delete" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="currentPage === 1">
        <el-form inline>
          <el-form-item label="属性名称"> <el-input placeholder="请输入属性名称" v-model="attrParams.attrName"></el-input>
          </el-form-item>
        </el-form>
        <el-button :disabled="attrParams.attrName ? false : true" @click="addAttrValue" type="primary"
          size="default">添加属性值</el-button>
        <el-button type="primary" size="default" @click="cancel">取消</el-button>
        <el-table border style="margin: 10px 0px" :data="attrParams.attrValueList">
          <el-table-column prop="id" label="序号" type="index" align="center" width="80"></el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="120px">
            <template #default="scope">
              <el-input placeholder="请输入属性值名称" v-model="scope.row.valueName"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" width="240px"></el-table-column>
        </el-table>
        <el-button type="primary" size="default" @click="save">保存</el-button>
        <el-button type="primary" size="default" @click="cancel">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang='ts'>
import { ref, reactive, onMounted, computed, watch } from 'vue'
// 引入仓库
import CategoryStore from '@/store/modules/category'
import { AttrResponseData, Attr } from '@/api/product/attr/type';
import { reqAddOrUpdateAttr, reqAttr } from '@/api/product/attr';
import { ElMessage } from 'element-plus';
let useCategoryStore = CategoryStore();
// 收集新增编辑数据
let attrParams = reactive<Attr>({
  attrName: "",//新增的属性的名字
  attrValueList: [],//新增的属性值数组
  categoryId: '',//三级分类的ID
  categoryLevel: 3,//代表的是三级分类
});
// 定义card组件切换页面
let currentPage = ref<number>(0);
// 添加属性值按钮事件页面增加列表
const addAttrValue = () => {
  attrParams.attrValueList.push({
    attrId: attrParams.id,
    valueName: '',
    flag: true
  });
};

// 使用计算属性来决定按钮是否禁用
const isButtonDisabled = computed(() => {
  return useCategoryStore.cat3Id === '' || useCategoryStore.cat3Id === null || useCategoryStore.cat3Id === undefined;
});

// 返回的属性值
let attrArr = ref<Attr[]>([]);
// 监听仓库三级分类id变化
watch(() => useCategoryStore.cat3Id, (newValue, oldValue) => {
  // 清空属性值标签
  attrArr.value = [];
  // 保证有三级分类id才发请求
  if (!useCategoryStore.cat3Id) return;
  getAttr();
});
// 获取已有的属性值
const getAttr = async () => {
  // 从仓库解构出分类id
  let { catId, cat2Id, cat3Id } = useCategoryStore;
  let result: AttrResponseData = await reqAttr(catId, cat2Id, cat3Id);
  if (result.code === 200) {
    console.log(result.data);
    attrArr.value = result.data;
  }
};

// 新增编辑
const saveOrUpdate = () => {
  currentPage.value = 1;
  //每一次点击的时候,先清空一下数据再收集数据
  Object.assign(attrParams, {
    attrName: "",//新增的属性的名字
    attrValueList: [],//新增的属性值数组
    categoryId: useCategoryStore.cat3Id,//三级分类的ID
    categoryLevel: 3,//代表的是三级分类
  })
};

// 保存
const save = async () => {
  let result: any = await reqAddOrUpdateAttr(attrParams);
  if (result.code === 200) {
    ElMessage.success(attrParams.id ? '修改成功':'添加成功');
    currentPage.value = 0;
    // 刷新获取三级分类对应的属性标签
    getAttr();
  } else {
    ElMessage.error('保存失败');
  }
};

// 取消
const cancel = () => {
  currentPage.value = 0;
}


</script>

<style lang='scss' scoped></style>